/*******************************************************************
 * author   :  Charlies-ls
 * Date     :  2021-09-26
 * Copyright (c) 2021. artestplay@outlook.com. All Rights Reserved.
 *******************************************************************/
#include "ax_set.h"
#include "com.h"
#include "ax_printf.h"
#include "ax_hashtable.h"
struct ax_set
{
    ax_hashtable* table;
};

ax_set *ax_set_create(int type)
{
    ax_set* s = ALLOC(ax_set);
    ax_check_and_return_zero(s, NULL);

    uint32_t flag = HT_VALUE_CONST;
    if(AX_SET_CONST_VALUE)
        flag |= HT_KEY_CONST;
    s->table = ax_hashtable_create(flag);
    if(NULL == s->table)
    {
        FREE(s);
        return NULL;
    }
    return s;
}

void ax_set_destory(ax_set *s)
{
    if(s)
    {
        ax_hashtable_destory(s->table);
        FREE(s);
    }
}

void ax_set_insert(ax_set *s, void *data, size_t data_size)
{
    static uint8_t ch = ' ';
    ax_check_and_return(s && data && data_size>0, NULL);
    ax_hashtable_insert(s->table, data, data_size, (void*)&ch, 1);
}

void ax_set_remove(ax_set *s, void *data, size_t data_size)
{
    ax_check_and_return(s && data && data_size>0, NULL);
    ax_hashtable_remove(s->table, data, data_size);
}

bool ax_set_contains(ax_set *s, void *data, size_t data_size)
{
    ax_check_and_return_false(s, NULL);
    return ax_hashtable_contains(s->table, data, data_size);
}

ax_hashtable *ax_set_hashtable(ax_set *s)
{
    ax_check_and_return_zero(s, NULL);
    return s->table;
}

size_t ax_set_size(ax_set *s)
{
    ax_check_and_return_zero(s, NULL);
    return ax_hashtable_size(s->table);
}

void ax_set_clear(ax_set *s)
{
    ax_check_and_return(s, NULL);
    ax_hashtable_clear(s->table);
}
